﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DblogStudio.DAL.Models;
using DblogStudio.GUI;
using DblogStudio.Utils;
namespace DblogStudio.BLL
{
    public partial class BizNhatKyHoatDong
    {
        /// <summary>
        /// Lấy danh sách
        /// </summary>
        /// <returns></returns>
        public static List<NhatKyHoatDong> GetList()
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.NhatKyHoatDongDbSet.OrderByDescending(e => e.ThoiGianGhiNhan).ToList();
            }
        }

        public static MySortableBindingList<NhatKyHoatDong> GetList(int loaiThaoTac, int loaiDoiTuong, string maNguoiThucHien, DateTime dtFrom, DateTime dtTo, int pageIndex, int pageSize)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                int skip = (pageIndex - 1) * pageSize;
                IQueryable<NhatKyHoatDong> ls = db.NhatKyHoatDongDbSet.Where(e => e.ThoiGianGhiNhan >= dtFrom && e.ThoiGianGhiNhan <= dtTo);
                if (loaiThaoTac == -1)
                    if (loaiDoiTuong == -1)
                        if (maNguoiThucHien == "-1")
                            ;
                        else
                            ls = ls.Where(e => e.MaNguoiThucHien == maNguoiThucHien);
                    else
                    {
                        if (maNguoiThucHien == "-1")
                            ls = ls.Where(e => e.LoaiDoiTuong == loaiDoiTuong);
                        else
                            ls = ls.Where(e => e.LoaiDoiTuong == loaiDoiTuong && e.MaNguoiThucHien == maNguoiThucHien);
                    }
                else
                {
                    if (loaiDoiTuong == -1)
                        if (maNguoiThucHien == "-1")
                            ls = ls.Where(e => e.LoaiThaoTac == loaiThaoTac);
                        else
                            ls = ls.Where(e => e.LoaiThaoTac == loaiThaoTac && e.MaNguoiThucHien == maNguoiThucHien);
                    else
                    {
                        if (maNguoiThucHien == "-1")
                            ls = ls.Where(e => e.LoaiThaoTac == loaiThaoTac && e.LoaiDoiTuong == loaiDoiTuong).OrderByDescending(e => e.ThoiGianGhiNhan);
                        else
                            ls = ls.Where(e => e.LoaiThaoTac == loaiThaoTac && e.LoaiDoiTuong == loaiDoiTuong && e.MaNguoiThucHien == maNguoiThucHien);
                    }
                }
                var rs = ls.OrderByDescending(e => e.ThoiGianGhiNhan).Skip(skip).Take(pageSize);
                MySortableBindingList<NhatKyHoatDong> lsSort = new MySortableBindingList<NhatKyHoatDong>(rs.ToList());
                return lsSort;
            }
        }

        public static NhatKyHoatDong GetByID(string maNhatKyGUID)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.NhatKyHoatDongDbSet.Where(e => e.MaNhatKyGUID == maNhatKyGUID).FirstOrDefault();
            }
        }

        /// <summary>
        /// Khởi tạo các giá trị cho biến nhatKy
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nhatKy"></param>
        /// <param name="duLieuCu">Để null nếu là thao tác thêm</param>
        /// <param name="maDoiTuong"></param>
        /// <param name="loaiDoiTuong"></param>
        /// <param name="loaiThaoTac"></param>
        public static void InitData<T>(NhatKyHoatDong nhatKy, T duLieuCu, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, LOAITHAOTAC loaiThaoTac)
        {
            nhatKy.LoaiDoiTuong = (int)loaiDoiTuong;
            nhatKy.LoaiThaoTac = (int)loaiThaoTac;
            if (duLieuCu != null)
                nhatKy.DuLieuCuXML = XMLConvertObject.Serialize<T>(duLieuCu);
            nhatKy.MaDoiTuong = maDoiTuong;
            nhatKy.MaNguoiThucHien = FormMain.NguoiSuDung.UserName;
            nhatKy.ThoiGianGhiNhan = DateTime.Now;
        }

        /// <summary>
        /// Thêm vào bảng nhật ký
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nhatKy"></param>
        /// <param name="duLieuMoi">Để null nếu là thao tác xóa</param>
        /// <returns></returns>
        public static CODE_RESULT_RETURN Add<T>(NhatKyHoatDong nhatKy, T duLieuMoi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                if (duLieuMoi != null)
                    nhatKy.DuLieuMoiXML = XMLConvertObject.Serialize<T>(duLieuMoi);
                nhatKy.MaNhatKyGUID = Guid.NewGuid().ToString();
                db.NhatKyHoatDongDbSet.Add(nhatKy);
                db.SaveChanges();
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        private static void LogAndCommit<T>(T duLieuCu, T duLieuMoi, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, LOAITHAOTAC loaiThaoTac, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
            {
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                {
                    LogAndCommit<T>(duLieuCu, duLieuMoi, maDoiTuong, loaiDoiTuong, loaiThaoTac, db2);
                }
            }
            else
            {
                try
                {
                    //var nhatKy = new NhatKyHoatDong();
                    //InitData<T>(nhatKy, duLieuCu, maDoiTuong, loaiDoiTuong, loaiThaoTac);
                    //if (duLieuMoi != null)
                    //    nhatKy.DuLieuMoiXML = XMLConvertObject.Serialize<T>(duLieuMoi);
                    //nhatKy.MaNhatKyGUID = System.Guid.NewGuid().ToString();
                    //db.NhatKyHoatDongDbSet.Add(nhatKy);
                    db.SaveChanges();
                }
                catch (Exception exc)
                {
                    throw new Exception(string.Format("Không thể {0} {1} được", loaiThaoTac, loaiDoiTuong), exc);
                }
            }
        }

        private static void LogAndCommit2<T>(T duLieuCu, T duLieuMoi, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, LOAITHAOTAC loaiThaoTac, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
            {
                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                {
                    LogAndCommit2<T>(duLieuCu, duLieuMoi, maDoiTuong, loaiDoiTuong, loaiThaoTac, db2);
                }
            }
            else
            {
                try
                {
                    var nhatKy = new NhatKyHoatDong();
                    InitData<T>(nhatKy, duLieuCu, maDoiTuong, loaiDoiTuong, loaiThaoTac);
                    if (duLieuMoi != null)
                        nhatKy.DuLieuMoiXML = XMLConvertObject.Serialize<T>(duLieuMoi);
                    nhatKy.MaNhatKyGUID = System.Guid.NewGuid().ToString();
                    db.NhatKyHoatDongDbSet.Add(nhatKy);
                    db.SaveChanges();
                }
                catch (Exception exc)
                {
                    throw new Exception(string.Format("Không thể {0} {1} được", loaiThaoTac, loaiDoiTuong), exc);
                }
            }
        }

        public static void LogAndCommitInsert<T>(T duLieuMoi, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, CEMS4UG_DHKHContext db = null)
        {
            LogAndCommit<T>(default(T), duLieuMoi, maDoiTuong, loaiDoiTuong, LOAITHAOTAC.Thêm, db);
        }

        public static void LogAndCommitInsert2<T>(T duLieuMoi, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, CEMS4UG_DHKHContext db = null)
        {
            LogAndCommit2<T>(default(T), duLieuMoi, maDoiTuong, loaiDoiTuong, LOAITHAOTAC.Thêm, db);
        }


        public static void LogAndCommitDelete<T>(T duLieuCu, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, CEMS4UG_DHKHContext db = null)
        {
            LogAndCommit<T>(duLieuCu, default(T), maDoiTuong, loaiDoiTuong, LOAITHAOTAC.Xóa, db);
        }

        public static void LogAndCommitDelete2<T>(T duLieuCu, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, CEMS4UG_DHKHContext db = null)
        {
            LogAndCommit2<T>(duLieuCu, default(T), maDoiTuong, loaiDoiTuong, LOAITHAOTAC.Xóa, db);
        }

        public static void LogAndCommitUpdate<T>(T duLieuCu, T duLieuMoi, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, CEMS4UG_DHKHContext db = null)
        {
            LogAndCommit<T>(duLieuCu, duLieuMoi, maDoiTuong, loaiDoiTuong, LOAITHAOTAC.Chỉnh_sửa, db);
        }

        public static void LogAndCommitUpdate2<T>(T duLieuCu, T duLieuMoi, string maDoiTuong, LOAIDOITUONG loaiDoiTuong, CEMS4UG_DHKHContext db = null)
        {
            LogAndCommit2<T>(duLieuCu, duLieuMoi, maDoiTuong, loaiDoiTuong, LOAITHAOTAC.Chỉnh_sửa, db);
        }

        public static CODE_RESULT_RETURN Delete(string maNhatKy)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.NhatKyHoatDongDbSet.Where(e => e.MaNhatKyGUID == maNhatKy).FirstOrDefault();
                if (_ph == null)
                    return CODE_RESULT_RETURN.ThatBai;
                db.NhatKyHoatDongDbSet.Remove(_ph);
                db.SaveChanges();
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }
}
